package com.softer.chengxin.dicinfo.controller;

import com.softer.chengxin.common.domin.CommonResult;
import com.softer.chengxin.common.domin.ManagerPageResult;
import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.common.enums.ResultEnum;
import com.softer.chengxin.dicinfo.entity.DicInfo;
import com.softer.chengxin.dicinfo.service.DicInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author zcz
 * @date 2018/12/20
 */
@RestController
@RequestMapping("/api/dicInfo")
@Slf4j
public class  DicInfoController {
    @Autowired
    private DicInfoService dicInfoService;

    /**
     * 查询列表
     * @param page
     * @param dicInfo
     * @return
     */
    @PostMapping("/getData")
    public ManagerPageResult getData(Page page, DicInfo dicInfo) {
        return dicInfoService.getData(page,dicInfo);
    }

    /**
     * 新增
     * @param dicInfo
     * @return
     */
    @PostMapping("/add")
    public CommonResult add(DicInfo dicInfo, HttpSession session){
        String loginName = (String) session.getAttribute("loginName");
        if (StringUtils.isBlank(loginName)) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "请重新登陆后操作");
        }
        dicInfo.setAdder(loginName);
        return dicInfoService.add(dicInfo);
    }

    /**
     * 修改
     * @param dicInfo
     * @return
     */
    @PostMapping("/update")
    public CommonResult update(DicInfo dicInfo,HttpSession session){
        String loginName = (String) session.getAttribute("loginName");
        if (StringUtils.isBlank(loginName)) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "请重新登陆后操作");
        }
        dicInfo.setAdder(loginName);
        return dicInfoService.update(dicInfo);
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    public CommonResult delete(String id,HttpSession session){
        String loginName = (String) session.getAttribute("loginName");
        if (StringUtils.isBlank(loginName)) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "请重新登陆后操作");
        }
        return dicInfoService.delete(id);
    }
    /**
     * 根据id查询字典信息
     * @param id
     * @return
     */
    @PostMapping("/getDicInfoById")
    public DicInfo getDicInfoById(String id) {
        return dicInfoService.getDicInfoById(id);
    }
    /**
     * 根据字典类型查询字典信息列表，填充下拉列表
     * @param typeCode
     * @return
     */
    @PostMapping("/getDicInfoByTypeCode")
    public List<DicInfo> getDicInfoByTypeCode(String typeCode){
        return dicInfoService.getDicInfoByTypeCode(typeCode);
    }
}
